package com.yanqu.road.server.manager.activity.commonwish;

import com.yanqu.road.dao.impl.activity.commonwish.CommonWishDrawNoticeDataDaoImpl;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.commonwish.CommonWishDrawNoticeData;
import com.yanqu.road.entity.enums.eActivityConfigType;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.pb.activity.CommonWishProto;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CommonWishDrawActivity {

    private ActivityInfo activityInfo;

    private List<CommonWishDrawNoticeData> noticeDataList;

    Map<String, ActivityConfig> activityConfigMap;

    private int noticeCount = 100;

    public void init(){

        List<Integer> activityIdList = new ArrayList<>();
        activityIdList.add(activityInfo.getActivityId());
        activityConfigMap = ActivityBussiness.getActivityConfigMap(activityIdList).get(activityInfo.getActivityId());

        if(activityConfigMap.containsKey(eActivityConfigType.XIUXIAN_NEW_DRAW_NOTICE.getName())){
            noticeCount = activityConfigMap.get(eActivityConfigType.XIUXIAN_NEW_DRAW_NOTICE.getName()).getIntValue();
        }

        noticeDataList = new CommonWishDrawNoticeDataDaoImpl().getCommonWishDrawNoticeData(activityInfo.getActivityId(), noticeCount);
        noticeDataList.sort((o1, o2) -> {
                    long l = o1.getGetTime() - o2.getGetTime();
            return (int)l;
        });
    }
    
    public void save(){

        for (int i = 0; i < noticeDataList.size(); i++) {
           CommonWishDrawNoticeData noticeData = noticeDataList.get(i);
            if(noticeData.isInsertOption()){
                new CommonWishDrawNoticeDataDaoImpl().add(noticeData);
            }else if(noticeData.isUpdateOption()){
                //new CommonWishDrawNoticeDataDaoImpl().update(noticeData);
            }
        }
    }

    public CommonWishDrawActivity(ActivityInfo activityInfo) {
        this.activityInfo = activityInfo;
    }

    public void addDrawNotice(long serverId, long playerId, List<CommonWishProto.CommonWishNoticeMsg> noticeList) {
        synchronized (noticeDataList){
            for (CommonWishProto.CommonWishNoticeMsg noticeMsg : noticeList) {
                CommonWishDrawNoticeData noticeData = new CommonWishDrawNoticeData();
                noticeData.setActivityId(activityInfo.getActivityId());
                noticeData.setGetTime(noticeMsg.getTime());
                noticeData.setServerId(serverId);
                noticeData.setUserId(playerId);
                noticeData.setNickName(noticeMsg.getNickName());
                noticeData.setReward(noticeMsg.getReward());
                noticeData.setParam(noticeMsg.getValue() + "");
                noticeData.setInsertOption();
                noticeDataList.add(noticeData);
            }
            while (noticeDataList.size() > noticeCount){
                noticeDataList.remove(0);
            }
        }
    }

    public CommonWishProto.CommonWishNoticeRespMsg.Builder getNoticeList() {

        CommonWishProto.CommonWishNoticeRespMsg.Builder respMsg = CommonWishProto.CommonWishNoticeRespMsg.newBuilder();

        List<CommonWishDrawNoticeData> list = noticeDataList;
        if(list != null){
            for (int i = 0; i < list.size(); i++) {
                CommonWishProto.CommonWishNoticeMsg.Builder builder = CommonWishProto.CommonWishNoticeMsg.newBuilder();
                CommonWishDrawNoticeData noticeData = list.get(i);
                builder.setTime(noticeData.getGetTime());
                builder.setReward(noticeData.getReward());
                builder.setServerId(noticeData.getServerId());
                builder.setNickName(noticeData.getNickName());
                builder.setValue(Integer.valueOf(noticeData.getParam()));
                respMsg.addNoticeList(builder);
            }
        }
        respMsg.setRet(0);
        respMsg.setActivityId(activityInfo.getActivityId());
        return respMsg;
    }
    
    public ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public void setActivityInfo(ActivityInfo activityInfo) {
        this.activityInfo = activityInfo;
    }

    public List<CommonWishDrawNoticeData> getNoticeDataList() {
        return noticeDataList;
    }

    public void setNoticeDataList(List<CommonWishDrawNoticeData> noticeDataList) {
        this.noticeDataList = noticeDataList;
    }
}
